package com.android.vgongyi.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import com.android.vgongyi.common.AppConstant;
import com.android.vgongyi.dao.BaseDAO;
import com.android.vgongyi.dao.CommentDAO;
import com.android.vgongyi.dao.CommonDAO;
import com.android.vgongyi.dao.FavorateDAO;
import com.android.vgongyi.dao.ImageDAO;

/**
 * data  base 
 * */
public class VGongyiProvider extends ContentProvider{
	
	private static final String TAG   = "VGongyiProvider";
	private final String DB_NAME      = "vgongyi";
	private final int DB_VERSION      = 5;
	
	private SQLiteDatabase db;
	private UriMatcher mUriMatcher;

	
	/**
	 * 
	 * set URI id in for created table
	 * */
	{
		
		mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		mUriMatcher.addURI(BaseDAO.AUTHORITIES, BaseDAO.TABLE_COMMON, AppConstant.NO_0);
		mUriMatcher.addURI(BaseDAO.AUTHORITIES, BaseDAO.TABLE_COMMENT, AppConstant.NO_1);
		mUriMatcher.addURI(BaseDAO.AUTHORITIES, BaseDAO.TABLE_IMAGE, AppConstant.NO_2);
		mUriMatcher.addURI(BaseDAO.AUTHORITIES, BaseDAO.TABLE_FAVORITE, AppConstant.NO_3);
		Log.i(TAG,"constructor");
				
	}
	
	
	private class DBHelper extends SQLiteOpenHelper{
		
		public DBHelper(Context context) {
			
			/* create database*/
			super(context, DB_NAME, null, DB_VERSION);	
			
		}

		/**
		 * create table
		 * */
		public void onCreate(SQLiteDatabase db) {
			
			CommonDAO commonDAO     = new CommonDAO(getContext());
			CommentDAO commentDAO   = new CommentDAO(getContext());
			ImageDAO imageDAO       = new ImageDAO(getContext());
			FavorateDAO favorateDAO = new FavorateDAO(getContext());
			
			/* clear the cache data which is saved in last time*/
			db.execSQL(commonDAO.dropTable());
			db.execSQL(commentDAO.dropTable());
			db.execSQL(imageDAO.dropTable());
			db.execSQL(favorateDAO.dropTable());
			/* create the new table*/
			db.execSQL(commonDAO.createTableString());
			db.execSQL(commentDAO.createTableString());
			db.execSQL(imageDAO.createTableString());
			db.execSQL(favorateDAO.createTableString());
			
		}

		
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			
			onCreate(db);

		}
		
	}
	
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		
		int matchCode = mUriMatcher.match(uri);
		
		switch (matchCode) {
		
			case AppConstant.NO_0:
				
				db.delete(BaseDAO.TABLE_COMMON, selection, selectionArgs);
				
				break;
			case AppConstant.NO_1:
				
				db.delete(BaseDAO.TABLE_COMMENT, selection, selectionArgs);
				
				break;
			case AppConstant.NO_2:
				
				db.delete(BaseDAO.TABLE_IMAGE, selection, selectionArgs);
				
				break;
			case AppConstant.NO_3:
				
				db.delete(BaseDAO.TABLE_FAVORITE, selection, selectionArgs);
				
				break;
	
		}
		
	    return 0;		
	}

	
	public String getType(Uri uri) {
		
		return uri.toString();
		
	}


	public Uri insert(Uri uri, ContentValues values) {

		int matchCode = mUriMatcher.match(uri);
		
		switch (matchCode) {
		
		case AppConstant.NO_0:
			
			db.insert(BaseDAO.TABLE_COMMON, null, values);
			
			break;
		case AppConstant.NO_1:
			
			db.insert(BaseDAO.TABLE_COMMENT, null, values);
			
			break;
	    case AppConstant.NO_2:
			
			db.insert(BaseDAO.TABLE_IMAGE, null, values);
			
			break;
	    case AppConstant.NO_3:
			
			db.insert(BaseDAO.TABLE_FAVORITE, null, values);
			
			break;

		}
		
		return null;
	}


	public boolean onCreate() {

		db = new DBHelper(getContext()).getWritableDatabase();
		return false;
		
	}

	public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {
		
		int matchCode = mUriMatcher.match(uri);
		Cursor cursor = null;
		
		switch (matchCode) {
				
			case AppConstant.NO_0:
				
				cursor = db.query(BaseDAO.TABLE_COMMON, null, selection, selectionArgs, null, null, null);
				
				break;
			case AppConstant.NO_1:
				
				cursor = db.query(BaseDAO.TABLE_COMMENT, null, selection, selectionArgs, null, null, null);
				
				break;
			case AppConstant.NO_2:
				
				cursor = db.query(BaseDAO.TABLE_IMAGE, null, selection, selectionArgs, null, null, null);
				
				break;
			case AppConstant.NO_3:
				
				cursor = db.query(BaseDAO.TABLE_FAVORITE, null, selection, selectionArgs, null, null, null);
				
				break;
	
		}
		
		return cursor;
	}


	public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {
	
		int matchCode = mUriMatcher.match(uri);
		
		switch (matchCode) {
		
			case AppConstant.NO_0:
	
				db.update(BaseDAO.TABLE_COMMON, values, selection, selectionArgs);
				
				break;
			case AppConstant.NO_1:
	
				db.update(BaseDAO.TABLE_COMMENT, values, selection, selectionArgs);
				
				break;
			case AppConstant.NO_2:
	
				db.update(BaseDAO.TABLE_IMAGE, values, selection, selectionArgs);
				
				break;
			case AppConstant.NO_3:
	
				db.update(BaseDAO.TABLE_FAVORITE, values, selection, selectionArgs);
				
				break;
		}
		
		return 0;
	}

}
